﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VirtualPress.Infrastructure.Entities.JSONModel;
using VirtualPress.Tool.Framework;
using VirtualPress.Portal.Biz;
using VirtualPress.Portal.Web.Models;
using VirtualPress.Infrastructure.Entities.Entity;
using VirtualPress.Infrastructure.Entities.Enum;
using VirtualPress.Portal.WebLogic;

namespace VirtualPress.Portal.Web.Controllers
{
    [Compress]
    [ErrorHandle]
    public class JobController : SiteBaseController
    {
        //
        // GET: /Job/

        public ActionResult Item(string token)
        {
            token = EncodeByBase64.Decode(token);
            EmailTaskJsonModel model = token.ToObject<EmailTaskJsonModel>();
            if (model == null)
            {
                return RedirectToAction("NoJobItem");
            }
            switch (model.Type)
            {
                case TaskType.NoticeToExpert:
                case TaskType.MeetingToExpert:
                    return RedirectToAction("ConfirmSpecialistInfo", new { type = (int)model.Type, token = EncodeByBase64.Encode(model.Stream) });
                case TaskType.NoticeToReviser:
                    return RedirectToAction("ConfirmUserInfo", new { type = (int)model.Type, token = EncodeByBase64.Encode(model.Stream) });

            }
            return RedirectToAction("NoJobItem");
        }
        public ActionResult NoJobItem()
        {
            return View();
        }
        #region 邀请加入编委会相关
        /// <summary>
        /// 对于站内专家。判断是否存在专家信息。
        /// </summary>
        /// <param name="type"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ActionResult ConfirmSpecialistInfo(int type, string token)
        {
            ConfirmInviteForExpertJsonModel invite = EncodeByBase64.Decode(token).ToObject<ConfirmInviteForExpertJsonModel>();
            if (!invite.Agree)
            {
                if (type == (int)TaskType.NoticeToExpert)
                {
                    JournalBiz.RefuseInviteExpert(invite.JournalLeaguerId, invite.JournalId, invite.ExpertAccount);
                }
                else if(type==(int)TaskType.MeetingToExpert)
                {
                    MeetingBiz.RefuseInviteExpert(invite.JournalLeaguerId, invite.JournalId, invite.ExpertAccount);
                }
                return RedirectToAction("RefuseJoinEditorial", new { journal = invite.JournalId });
            }
            Organization org = null;
            if (type == (int)TaskType.NoticeToExpert)
            {
               org = new Organization().Find(c => c.Id == invite.JournalLeaguerId && c.OrgType == (int)OrganizationType.Journal && c.OrgId == invite.JournalId && c.UserName == invite.ExpertAccount
                && c.Status == (int)JournalStatus.Counsellor && c.ApplyStatus == 0);
            }
            else if (type == (int)TaskType.MeetingToExpert)
            {
                org = new Organization().Find(c => c.Id == invite.JournalLeaguerId && c.OrgType == (int)OrganizationType.Meeting && c.OrgId == invite.JournalId && c.UserName == invite.ExpertAccount
                   && c.Status == (int)JournalStatus.Counsellor && c.ApplyStatus == 0);
            }
            if (org == null)
            {
                return RedirectToAction("NoJobItem");
            }
            var expert = new Expert().Find(c => c.UserName == invite.ExpertAccount);
            if (expert == null)
            {
                return RedirectToAction("SpecialistInfo", new { type = type, token = token });
            }
            else
            {
                //存在专家信息
                return RedirectToAction("ModeSpecialist", new { type = type, token = token });
            }
        }
        /// <summary>
        /// 对于站外专家，需要注册专家信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public ActionResult SpecialistInfo(int type, string token)
        {
            ConfirmInviteForExpertJsonModel invite =  EncodeByBase64.Decode(token).ToObject<ConfirmInviteForExpertJsonModel>();
            //var user = new User().Any(c=>c.UserName == invite.ExpertAccount);
            //ViewBag.HasUser = user;
            ViewBag.Type = type;
            ViewBag.Token = token;
            return View();
        }
        public ActionResult ModeSpecialist(int type, string token, SpecialistInfoModel model = null)
        {
            if (String.IsNullOrEmpty(model.Title))
            {
                model = null;
            }
            ConfirmInviteForExpertJsonModel invite = EncodeByBase64.Decode(token).ToObject<ConfirmInviteForExpertJsonModel>();
            if (invite == null)
            {
                return RedirectToAction("NoJobItem");
            }
            User user = null;
            Expert expert = null;
            if (model != null)
            {
                user = new User().Find(invite.ExpertAccount);
                if (user == null)
                {
                    user = new User()
                    {
                        Area = (AreaCode)model.AreaCode,
                        Birthday = model.Birthday,
                        Email = invite.ExpertAccount,
                        Gender = (Gender)model.Gender,
                        IsActivate = true,
                        IsRefuse = false,
                        Location = (Location)model.Location,
                        NickName = model.NickName,
                        Password = model.Password,
                        Profession = (Profession)model.Profession,
                        RcdDate = DateTime.Now,
                        SystemRole = UserRole.Expert,
                        UserName = invite.ExpertAccount,
                        Phone = model.Phone
                    };
                    UserBiz.Register(user, true);
                }
                else
                {
                    user.Area = (AreaCode)model.AreaCode;
                    user.Birthday = model.Birthday;
                    user.Email = invite.ExpertAccount;
                    user.Gender = (Gender)model.Gender;
                    user.IsActivate = true;
                    user.IsRefuse = false;
                    user.Location = (Location)model.Location;
                    user.NickName = model.NickName;
                    user.Password = AesAlgorithm.Encrypt(model.Password);
                    user.Profession = (Profession)model.Profession;
                    user.RcdDate = DateTime.Now;
                    user.SystemRole = UserRole.Expert;
                    user.Phone = model.Phone;
                    user.PostModify();
                }
                expert = new Expert().Find(invite.ExpertAccount);
                if (expert == null)
                {
                    expert = new Expert()
                    {
                        Degree = model.Degree,
                        Institution = model.Institution,
                        Introduction = model.Introduction,
                        Profession = (Profession)model.Profession,
                        Title = model.Title,
                        UserName = invite.ExpertAccount,
                        Isvalid = true
                    }.PostAdd();
                }
                else
                {
                    expert.Degree = model.Degree;
                    expert.Institution = model.Institution;
                    expert.Introduction = model.Introduction;
                    expert.Profession = (Profession)model.Profession;
                    expert.Title=model.Title;
                    expert.Isvalid =true;
                    expert.PostModify();
                }
            }

            if (JournalBiz.ConfirmInviteForExpert((TaskType)type, invite.Agree, invite.ExpertAccount, invite.JournalId, invite.JournalLeaguerId,
                    user, expert))
            {
                return RedirectToAction("AgreeJoinEditorial", new { journal = invite.JournalId });
            }
            return RedirectToAction("NoJobItem");
        }
        #endregion

        #region 邀请加入初审相关
        public ActionResult ConfirmUserInfo(int type, string token)
        {
            ConfirmInviteForExpertJsonModel invite = EncodeByBase64.Decode(token).ToObject<ConfirmInviteForExpertJsonModel>();
            if (!invite.Agree)
            {
                JournalBiz.RefuseInviteExpert(invite.JournalLeaguerId, invite.JournalId, invite.ExpertAccount);
                return RedirectToAction("RefuseJoinEditorial", new { journal = invite.JournalId });
            }
            var org = new Organization().Find(c => c.OrgId == invite.JournalId && c.UserName == invite.ExpertAccount && c.Status == (int)JournalStatus.Reviser);
            if (org == null)
            {
                return RedirectToAction("NoJobItem");
            }
            var user = new User().Find(invite.ExpertAccount);
            if (user == null)
            {
                return RedirectToAction("BuildUserInfo", new { type = type, token = token });
            }
            else
            {
                return RedirectToAction("ModeReviser", new { type = type, token = token });
            }
        }
        public ActionResult BuildUserInfo(int type, string token)
        {
            ViewBag.Type = type;
            ViewBag.Token = token;
            return View();
        }
        //public ActionResult ModeReviser(int type, string token, SpecialistInfoModel model)
        //{
        //    if (String.IsNullOrEmpty(model.Password))
        //    {
        //        model = null;
        //    }
        //    ConfirmInviteForExpertJsonModel invite = EncodeByBase64.Decode(token).ToObject<ConfirmInviteForExpertJsonModel>();
        //    if (invite == null)
        //    {
        //        return RedirectToAction("NoJobItem");
        //    }
        //    VirtualPress.Infrastructure.Entities.Entity.User user = null;
        //    if (model != null)
        //    {
        //        user = new User()
        //        {
        //            Area = (AreaCode)model.AreaCode,
        //            Birthday = model.Birthday,
        //            Email = invite.ExpertAccount,
        //            Gender = (Gender)model.Gender,
        //            IsActivate = true,
        //            IsRefuse = false,
        //            Location = (Location)model.Location,
        //            NickName = model.NickName,
        //            Password = model.Password,
        //            RcdDate = DateTime.Now,
        //            SystemRole = UserRole.Member,
        //            UserName = invite.ExpertAccount,
        //            Phone = model.Phone
        //        };
        //    }

        //    if (JournalBiz.ConfirmInviteForReviser(invite.Agree, invite.ExpertAccount, invite.JournalId, invite.JournalLeaguerId,
        //            user))
        //    {
        //        return RedirectToAction("AgreeJoinEditorial", new { journal = invite.JournalId });
        //    }
        //    return RedirectToAction("NoJobItem");
        //}
        #endregion

        public ActionResult RefuseJoinEditorial(int journal)
        {
            var item = new Periodical().Find(journal.ToString());
            if (item != null)
            {
                ViewBag.Name = item.Name;
            }
            return View();
        }
        public ActionResult AgreeJoinEditorial(int journal)
        {
            var item = new Periodical().Find(journal.ToString());
            if (item != null)
            {
                ViewBag.Name = item.Name;
            }
            return View();
        }

    }
}
